jQuery(document).ready(function () {

    "use strict";

    var loan_display_size = 5, // 默认显示5条数据
        loanTemplate = '<tr><td><i class="fa fa-circle iconColor fs8 pr15"></i><span><a href="loan/detail/loanId" target="_blank" class="text-muted-darker">loanTitle</a></span></td><td>loanAmount</td></tr>';

    /**
     * 载入标的数据
     * @param {type} status
     * @returns {undefined}
     */
    function loadLoans(status) {
        var url = "loan/getData/" + status + "?length=" + loan_display_size,
            tbody = $("tbody#" + status),
            num = $("span#size_of_" + status);

        $.get(url, function (res) {
            var loans = res.data;
            //num.text(res.recordsTotal);
            if (loans.length > 0) {
                tbody.html("");
                for (var i = 0; i < loans.length; i++) {
                    var loan = loans[i].loan, amountTd;
                    var tr = loanTemplate
                        .replace("iconColor", loan_status[status])
                        .replace("loanTitle", loan.title)
                        .replace("loanId", loan.id);

                    amountTd = "￥" + $.number(loan.amount);
                    if (status === 'OPEN' || status === 'FAILED') {
                        var percent = $.number(loan.investAmount * 100 / loan.amount, 0);
                        amountTd = $.number(loan.investAmount) + "/" + $.number(loan.amount);
                        amountTd += '<code class="ml5 fw200">进度：' + percent + '%</code>';
                    } else if (status === 'SCHEDULED') {
                        amountTd += '<code class="ml5 fw200">' + $.format.date(new Date(loan.openTime), "MM/dd HH:mm") + '</code>';
                    }
                    tr = tr.replace("loanAmount", amountTd);
                    $(tr).appendTo(tbody);
                }
            }
        }).fail(function () {
            alert("载入标的数据失败");
        });
    }

    function loadStatistics() {
        $.get("statistic/platform", function (res) {
            if (res) {
                $("#regCount").html($.number(res.regCount));
                $("#loginCount").html($.number(res.loginCount));
                $("#newLoanCount").html($.number(res.newLoanCount));
                $("#totalDeposit").html($.number(res.totalDeposit));
                $("#totalWithdraw").html($.number(res.totalWithdraw));
            } else {
                $("#regCount").html('0');
                $("#loginCount").html('0');
                $("#newLoanCount").html('0');
                $("#totalDeposit").html('0');
                $("#totalWithdraw").html('0');
            }
        }).fail(function () {
            alert("读取平台统计数据失败");
        });
    }

    /**
     * 首页显示的标的状态
     * @type type
     */
    var loan_status = {
        PROPOSED: "text-system", //待审核,
        APPROVED: "text-info", //待排期,
        OPEN: "text-success", //开放投标,
        SCHEDULED: "text-warning", //待发布,
        FINISHED: "text-alert", //待结算
        FAILED: "text-danger lighter" //流标
    };

    // 加载数据
    for (var status in loan_status) {
        loadLoans(status);
    }

    loadStatistics();
});